package com.a.lib_ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.a.lib_ui.R;
import com.a.lib_ui.activity.PopWindowsActivity;
import com.a.lib_ui.adapter.GridItemAdapter;
import com.a.lib_ui.bean.ChildClassType;
import com.a.lib_ui.bean.GridItem;
import com.a.lib_ui.bean.Packaging;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BottomWindowsFragment extends Fragment implements View.OnClickListener{
    private static final String TAG = BottomWindowsFragment.class.getSimpleName();

    private Context context;
    private GridView gv_type,gv_packing;
    private List<Map<String,Object>> typeList ,packingList;

    private  List<ChildClassType> childTypeNameList;
    private List<Packaging> packagingNameList;

    private  Set<String> childTypeNameSet;
    private Set<String> packagingNameSet;

    private Button btn_reset,btn_search;

    private SimpleAdapter adapter;

    private EditText et_factoryNo,et_productName,et_provider,et_productNo;

   private GridItemAdapter typeAdapter;
   private GridItemAdapter packagingAdapter;

   private PopWindowsActivity popWindowsActivity;

    private List<GridItem> gridItemList = new ArrayList<>(),gridItemList2 = new ArrayList<>();

    public static BottomWindowsFragment newInstance(List<ChildClassType> childTypeNames, List<Packaging> packagingList) {

        BottomWindowsFragment fragment = new BottomWindowsFragment();

        Bundle args = new Bundle();

        ArrayList list = new ArrayList();
        list.add(childTypeNames);
        list.add(packagingList);

        args.putSerializable("childTypeList",list);

        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.core_ui_fragment_bottom_windows, null);
        if (getArguments() != null) {
            ArrayList list =(ArrayList) getArguments().getSerializable("childTypeList");

            childTypeNameList= (List<ChildClassType>) list.get(0);
            packagingNameList = (List<Packaging>) list.get(1);


        }

        initView(view);
        initClickListener();
        initData();
        initGridView();
        return view;
    }

    public void initView(View view){

        gv_type = view.findViewById(R.id.gv_type);
        gv_packing = view.findViewById(R.id.gv_packing);
        btn_reset = view.findViewById(R.id.btn_reset);
        btn_search = view.findViewById(R.id.btn_search);

        et_factoryNo = view.findViewById(R.id.et_factoryNo);
        et_productName=view.findViewById(R.id.et_productName);
        et_provider = view.findViewById(R.id.et_provider);
        et_productNo = view.findViewById(R.id.et_productNo);

    }

    public void initData(){
        context = getContext();

        popWindowsActivity = (PopWindowsActivity) getActivity();
      // String name[]={"时钟向","信号非i","宝箱x","秒钟tt","大xxx","FF","记本","书签响","印象","主体商店","主题","迅雷不急"};

     /*   typeList = new ArrayList<>();
        for (int i = 0; i < childTypeNameList.size(); i++) {
            String name = childTypeNameList.get(i);
            Map<String,Object> map = new HashMap<>();
            map.put("type",name);
            typeList.add(map);
        }

        packingList = new ArrayList<>();
        for (int i = 0; i < packagingNameList.size(); i++) {
            String name = packagingNameList.get(i);
            Map<String,Object> map = new HashMap<>();
            map.put("type",name);
            packingList.add(map);
        }*/
        GridItem firstItem = new GridItem();
        firstItem.setTitle("全部");
        firstItem.setChecked(true);

        GridItem firstItem2 = new GridItem();
        firstItem2.setTitle("全部");
        firstItem2.setChecked(true);

        for (int i = 0; i < childTypeNameList.size(); i++) {
            GridItem item = new GridItem();
            item.setTitle(childTypeNameList.get(i).getCclassTypeName());
            gridItemList.add(item);


        }
        for (int i = 0; i < packagingNameList.size(); i++) {
            GridItem item = new GridItem();
            item.setTitle(packagingNameList.get(i).getPackName());
            gridItemList2.add(item);
        }
        gridItemList.add(0,firstItem);
        gridItemList2.add(0,firstItem2);

    }

    public void initGridView(){
       /* String [] from = {"type"};
        int [] to = {R.id.tv_tab};
        adapter = new SimpleAdapter(context,packingList,R.layout.item_gridview,from,to);
        typeAdapter =  new SimpleAdapter(context,typeList,R.layout.item_gridview,from,to);*/

         typeAdapter = new GridItemAdapter(context,R.layout.core_ui_item_gridview,gridItemList);
         packagingAdapter = new GridItemAdapter(context,R.layout.core_ui_item_gridview,gridItemList2);
        gv_type.setAdapter(typeAdapter);
        gv_packing.setAdapter(packagingAdapter);
       // gv_packing.setAdapter(adapter);

   /*     gv_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.d(TAG,"我点了也+"+position);
                TextView item = gv_type.getChildAt(position).findViewById(R.id.tv_tab);
                item.setBackgroundResource(R.drawable.grid_item_change);
                item.setTextColor(context.getResources().getColor(R.color.white));
                for(int i=0;i<parent.getChildCount();i++){

                    if (position == i) {//当前选中的Item改变背景颜色
                        item.setBackgroundResource(R.drawable.grid_item_change);
                        item.setTextColor(context.getResources().getColor(R.color.white));
                        //item
                    } else {
                        item.setBackgroundResource(R.drawable.grid_item);
                        item.setTextColor(context.getResources().getColor(R.color.darkgrey));
                    }
                }

            }
        });
        gv_packing.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.d(TAG,"我点了也+"+position);
                TextView item = gv_packing.getChildAt(position).findViewById(R.id.tv_tab);
                item.setBackgroundResource(R.drawable.grid_item_change);
                item.setTextColor(context.getResources().getColor(R.color.white));
                for(int i=0;i<parent.getCount();i++){
                    TextView item = gv_packing.getChildAt(i).findViewById(R.id.tv_tab);
                    if (position == i) {//当前选中的Item改变背景颜色
                        item.setBackgroundResource(R.drawable.grid_item_change);
                        item.setTextColor(context.getResources().getColor(R.color.white));
                        //item
                    } else {
                        item.setBackgroundResource(R.drawable.grid_item);
                        item.setTextColor(context.getResources().getColor(R.color.darkgrey));
                    }
                }

            }
        });*/

    }
    public void initClickListener(){
        btn_reset.setOnClickListener(this);
        btn_search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_reset) {
            doReset();
        } else if (id == R.id.btn_search) {
            doSearch();
        }
    }
    public void doSearch(){//点击搜索

        ArrayList<String> classTypeNoList = new ArrayList<>();
        ArrayList<String> packNoList = new ArrayList<>();

        Map<Integer,Boolean> typeMap = typeAdapter.getMap();
        Map<Integer,Boolean> packagingMap = packagingAdapter.getMap();

        if(typeMap!=null){
            if(!typeMap.containsKey(0)){
               for(int position : typeMap.keySet()){
                   String typeNo  = childTypeNameList.get(position-1).getCclassTypeNo();
                   classTypeNoList.add(typeNo);
               }
            }

        }

        if(packagingMap!=null){
            if(!packagingMap.containsKey(0)){

                for(int position : packagingMap.keySet()){
                    String typeNo  = packagingNameList.get(position).getPackID();
                    packNoList.add(typeNo);

                }
            }

        }


        Intent intent =new Intent();

        Log.d(TAG,"classTypeNoList========="+classTypeNoList.toString());

        Log.d(TAG,"packNoList========="+packNoList.toString());



            intent.putExtra("factory",et_factoryNo.getText()+"" );

            intent.putExtra("productName",et_productName.getText()+"" );

            intent.putExtra("provider",et_provider.getText()+"" );

            intent.putExtra("productNo",et_productNo.getText()+"" );



        String name = "再见";
        intent.putExtra("我毁灭了",name);
        intent.putStringArrayListExtra("typeNo",classTypeNoList);
        intent.putStringArrayListExtra("packNo",packNoList);


        popWindowsActivity.setResult(2,intent);
        popWindowsActivity.finish();
    }
    public void doReset(){
        for (int i = 1; i < gridItemList.size(); i++) {
            GridItem item  = gridItemList.get(i);
                item.setChecked(false);
        }
        for (int i = 1; i < gridItemList2.size(); i++) {
            gridItemList2.get(i).setChecked(false);
        }

        gridItemList.get(0).setChecked(true);
        gridItemList2.get(0).setChecked(true);

        typeAdapter = new GridItemAdapter(context,R.layout.core_ui_item_gridview,gridItemList);
        packagingAdapter = new GridItemAdapter(context,R.layout.core_ui_item_gridview,gridItemList2);

        gv_type.setAdapter(typeAdapter);

        gv_packing.setAdapter(packagingAdapter);


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
